home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / vopts / vopts.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  7KB  |  307 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6. #include "vopts.h"
  7. #include "vopts_rev.h"
  8.  
  9. Prototype int CreateWindow(void);
  10. Prototype void DestroyWindow(void);
  11. Prototype char *SkipAss(const char *);
  12.  
  13. void myexit(void);
  14. int main(int, char **);
  15.  
  16. struct GLOBAL global;
  17.  
  18. struct Library *GadToolsBase;
  19. struct Library *AslBase;
  20. struct Library *ArpBase;
  21.  
  22. BPTR XSaveLock;
  23. short XSaveLockValid;
  24.  
  25. char    *InitialFileName;
  26. short   FileSpecified;
  27. char    Buf[256];
  28. short   didgadgets;
  29.  
  30. const char version[] = VERSTAG;
  31.  
  32. /*
  33.  *  WORKBENCH STARTUP
  34.  *
  35.  *  Parse tooltypes as a command.  Note that we are CD'd into the
  36.  *  application's directory, not the tooltype's icon's dir.
  37.  */
  38.  
  39. void
  40. myexit()
  41. {
  42.    if (XSaveLockValid) {
  43.       CurrentDir(XSaveLock);
  44.       XSaveLockValid = 0;
  45.    }
  46. }
  47.  
  48. int
  49. wbmain(wbs)
  50. struct WBStartup *wbs;
  51. {
  52.    DiskObject *dob;
  53.    short i;
  54.    short j;
  55.    short abortIt = 0;
  56.  
  57.    /*
  58.     *  Search for options, set current directory to last valid
  59.     *  disk object.  Run commands specified by tool types in order
  60.     */
  61.  
  62.    for (i = 0; i < wbs->sm_NumArgs; ++i)
  63.    {
  64.       BPTR saveLock = CurrentDir((BPTR)wbs->sm_ArgList[i].wa_Lock);
  65.  
  66.       if (i == wbs->sm_NumArgs - 1 && FileSpecified == 0)
  67.       {
  68.          InitialFileName = strdup(wbs->sm_ArgList[i].wa_Name);
  69.       }
  70.  
  71.       if (dob = GetDiskObject(wbs->sm_ArgList[i].wa_Name))
  72.       {
  73.          for (j = 0; dob->do_ToolTypes[j]; ++j)
  74.          {
  75.             char *ptr = dob->do_ToolTypes[j];
  76.  
  77.             if (strnicmp(ptr, "FILE=", 5) == 0)
  78.             {
  79.                InitialFileName = strdup(SkipAss(ptr));
  80.                FileSpecified = 1;
  81.             }
  82.             else
  83.             {
  84.                /* OK to use any text slot, we're about to bail out */
  85.                strcpy(global.text[1], "Bad ToolType:");
  86.                if (request(0, 1, ptr, NULL))
  87.                {
  88.                   abortIt = 1;
  89.                   break;
  90.                }
  91.             }
  92.          }
  93.          FreeDiskObject(dob);
  94.       }
  95.       CurrentDir(saveLock);
  96.       if (abortIt)
  97.           break;
  98.    }
  99.    XSaveLock = CurrentDir((BPTR)wbs->sm_ArgList[wbs->sm_NumArgs-1].wa_Lock);
  100.    XSaveLockValid = 1;
  101.    atexit(myexit);
  102.  
  103.    if (abortIt == 0)
  104.       main(1, NULL);
  105. }
  106.  
  107. /*
  108.  *  CLI STARTUP
  109.  *
  110.  */
  111.  
  112. int
  113. main(argc, argv)
  114. int argc;
  115. char **argv;
  116. {
  117.    struct IntuiMessage *msg;
  118.    int parse_fail;
  119.  
  120.    /*
  121.     *   Open GadToolsBase/AslBase manually so we don't exit if they
  122.     *   don't exist.
  123.     */
  124.  
  125.     if (argc != 1) {
  126.         InitialFileName = argv[1];
  127.         FileSpecified = 1;
  128.     }
  129.  
  130.    GadToolsBase = OpenLibrary("gadtools.library", 0);
  131.    AslBase      = OpenLibrary("asl.library", 0);
  132.  
  133.    memset(&global, 0, sizeof(global));
  134.  
  135.    FillIn_RenderInfo(&global.ri, NULL);
  136.  
  137.    /* Provide a default height/width so that we will get any errors displayed well */
  138. /*
  139.    global.width = 320;
  140. */
  141.    global.width = (3 * global.ri.ScreenWidth) / 4;
  142.    global.height = global.ri.WindowTitle;
  143.  
  144.    global.ri.WindowTitle  += MARGIN_TOP;
  145.    global.ri.WindowLeft   += MARGIN_LEFT;
  146.    global.ri.WindowRight  += MARGIN_RIGHT;
  147.    global.ri.WindowBottom += MARGIN_BOTTOM;
  148.  
  149.    if (AslBase != NULL)
  150.       global.freq = AllocFileRequest();
  151.    else
  152.    {
  153.       ArpBase = OpenLibrary("arp.library", 0);
  154.       if (ArpBase != NULL)
  155.          global.freq = ArpAllocFreq();
  156.    }
  157.  
  158.    parse_fail = parse_config(ALT_CONFIG_FILE);
  159.    if (parse_fail == 2) /* failed to open file */
  160.       parse_fail = parse_config(CONFIG_FILE);
  161.    if ((!parse_fail) &&
  162.        (!init_gad_sizes()))
  163.    {
  164.       init_gadgets();
  165.       didgadgets = 1;
  166.    }
  167.    else
  168.    {
  169.       didgadgets = 0;
  170.    }
  171.  
  172.    if (!CreateWindow())
  173.    {
  174.       if (didgadgets)
  175.       {
  176.          set_gadgets(1);
  177.          do_command("READ ENV");
  178.  
  179.          if (FileSpecified) {
  180.             sprintf(Buf, "READ %s", InitialFileName);
  181.             do_command(Buf);
  182.          }
  183.       }
  184.  
  185.       while(!global.done)
  186.       {
  187.          WaitPort(global.window->UserPort);
  188.          while(msg = (struct IntuiMessage *)GetMsg(global.window->UserPort))
  189.          {
  190.             ULONG class;
  191.             APTR iaddr;
  192.             USHORT code;
  193.             USHORT qual;
  194.             struct XItem *xitem;
  195.  
  196.             code = msg->Code;
  197.             class = msg->Class;
  198.             iaddr = msg->IAddress;
  199.             qual  = msg->Qualifier;
  200.  
  201.             ReplyMsg((struct Message *)msg);
  202.  
  203.             switch(class)
  204.             {
  205.                case CLOSEWINDOW:
  206.                   global.done = 1;
  207.                   break;
  208.                case GADGETDOWN:
  209.                case GADGETUP:
  210.                   handle_hit((struct Gadget *)iaddr,
  211.                       (qual & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)) ? 1 : 0);
  212.                   break;
  213.                case MENUPICK:
  214.                   while (code != MENUNULL)
  215.                   {
  216.                      xitem = (struct XItem *)
  217.                              ItemAddress(global.menu, code);
  218.                      do_command(xitem->userdata);
  219.                      code = xitem->item.NextSelect;
  220.                   }
  221.                   break;
  222.                case VANILLAKEY:
  223. #ifdef DBG_JGM
  224.                   printf("Key:%d '%c'\n", code, code);
  225. #endif
  226.                   break;
  227.             }
  228.          }
  229.       }
  230.       DestroyWindow();
  231.    }
  232.  
  233.    CleanUp_RenderInfo(&global.ri);
  234.    if (global.freq && (AslBase != NULL)) FreeFileRequest(global.freq);
  235.  
  236.    if (GadToolsBase  != NULL) CloseLibrary(GadToolsBase);
  237.    if (AslBase  != NULL)      CloseLibrary(AslBase);
  238.    if (ArpBase  != NULL)      CloseLibrary(ArpBase);
  239.    return(0);
  240. }
  241.  
  242. /*
  243.  * Create a new window on the screen
  244.  */
  245. int CreateWindow()
  246. {
  247.    struct NewWindow nw;
  248.  
  249.    memset(&nw, 0, sizeof(nw));
  250.  
  251.    nw.Width      = global.width;
  252.    nw.Height     = global.height;
  253.    nw.LeftEdge   = (global.ri.ScreenWidth  - nw.Width ) / 2;
  254.    nw.TopEdge    = (global.ri.ScreenHeight - nw.Height) / 2;
  255.  
  256.    if ((nw.Width < 0) || (nw.Height < 0))
  257.       return(1);
  258.    nw.DetailPen  = nw.BlockPen = -1;
  259.    nw.IDCMPFlags = GADGETUP    |
  260.                    GADGETDOWN  |
  261.                    MENUPICK    |
  262.                    VANILLAKEY  |
  263.                    CLOSEWINDOW;
  264.    nw.Flags      = WINDOWDEPTH    |
  265.                    WINDOWCLOSE    |
  266.                    WINDOWDRAG     |
  267.                    SIMPLE_REFRESH |
  268.                    ACTIVATE       |
  269.                    NOCAREREFRESH;
  270.    nw.Title      = global.title;
  271.    nw.Type       = WBENCHSCREEN;
  272.  
  273.    global.window = OpenWindow(&nw);
  274.    if (global.window == NULL) return(2);
  275.    global.rp = global.window->RPort;
  276.    SetWindowTitles(global.window, global.title, global.wtitle);
  277.    enable_menu();
  278.    return(0);
  279. }
  280.  
  281. /*
  282.  * Make the current window go away
  283.  */
  284. void DestroyWindow()
  285. {
  286.    free_menus(global.menu);
  287.    CloseWindow(global.window);
  288. }
  289.  
  290. /*
  291.  *  Skip whitespace in tooltype assignment
  292.  */
  293.  
  294. char *
  295. SkipAss(ptr)
  296. const char *ptr;
  297. {
  298.    while (*ptr && *ptr != '=')
  299.       ptr++;
  300.    if (*ptr == '=')
  301.    {
  302.       for (ptr++; *ptr == ' ' || *ptr == '\t'; ptr++)
  303.          ;
  304.    }
  305.    return(ptr);
  306. }
  307.